﻿using System;
using Microsoft.SPOT;
using Gadgeteer.Modules.GHIElectronics;

namespace Gadgeteer.Interfaces.GHI.Btn
{
    internal class ButtonWrapper : IButton
    {
        private readonly Button button;

        public ButtonWrapper(Button button)
        {
            if (button == null) throw new ArgumentNullException("button");
            this.button = button;

            button.ButtonPressed += button_ButtonPressed;
            button.ButtonReleased += button_ButtonReleased;
        }

        public bool IsPressed
        {
            get { return button.IsPressed; }
        }

        private static ButtonState Translate(Button.ButtonState value)
        {
            switch (value)
            {
                case Button.ButtonState.Pressed:
                    return ButtonState.Pressed;
                case Button.ButtonState.Released:
                    return ButtonState.Released;
                default:
                    throw new NotImplementedException("This button state is inknown");
            }
        }

        void button_ButtonReleased(Button sender, Button.ButtonState state)
        {
            var ev = ButtonPressed;
            if (ev != null)
                ev(this, Translate(state));
        }

        void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            var ev = ButtonReleased;
            if (ev != null)
                ev(this, Translate(state));
        }

        public event ButtonEventHandler ButtonPressed;

        public event ButtonEventHandler ButtonReleased;
    }
}
